// by Xeno
#define THIS_FILE "fn_beam_tele.sqf"
#include "x_setup.sqf"
#define __CTRL2(A) (_display displayCtrl A)
private ["_display", "_ctrl", "_typepos","_global_pos","_global_dir","_leader","_camps"];
if (!X_Client || {GVAR(beam_target) < 0} || {GVAR(x_loop_end)}) exitWith {};

GVAR(x_loop_end) = true;

if (vehicle player != player) then {unassignVehicle player};

disableSerialization;
_display = __uiGetVar(X_TELE_DIALOG);
__CTRL2(100102) ctrlEnable false;
__CTRL2(100107) ctrlEnable false;
__CTRL2(100108) ctrlEnable false;
__CTRL2(100109) ctrlEnable false;
__CTRL2(100112) ctrlShow true;

GVAR(last_telepoint) = GVAR(beam_target);
_global_pos = [];
_global_dir = 180;
_typepos = 0;
switch (GVAR(beam_target)) do {
	case 0: {
#ifndef __TT__
		_global_pos = markerPos "base_spawn_1";
#else
		_global_pos = if (GVAR(player_side) == blufor) then {markerPos "base_spawn_1"} else {markerPos "base_spawn_2"};
#endif
	};
	case 1: {
#ifndef __TT__
		_global_pos = MRR1 modelToWorld [0,-7,0];
		_global_pos set [2,0];
		_global_dir = direction MRR1;
#else
		_global_pos = if (GVAR(player_side) == blufor) then {MRR1 modelToWorld [0,-7,0]} else {MRRR1 modelToWorld [0,-7,0]};
		_global_dir = if (GVAR(player_side) == blufor) then {direction MRR1} else {direction MRRR1};
#endif
		_typepos = 1;
	};
	case 2: {
#ifndef __TT__
		_global_pos = MRR2 modelToWorld [0,-7,0];
		_global_pos set [2,0];
		_global_dir = direction MRR2;
#else
		_global_pos = if (GVAR(player_side) == blufor) then {MRR2 modelToWorld [0,-7,0]} else {MRRR2 modelToWorld [0,-7,0]};
		_global_dir = if (GVAR(player_side) == blufor) then {direction MRR2} else {direction MRRR2};
#endif
		_typepos = 1;
	};
	// TYRGHEN
	case 3: {
		diag_log format ["T%1,DT%2,F%3,FPS%4,%5,%6,'%7'", time, diag_tickTime, diag_frameno, diag_fps, THIS_FILE_, __LINE__, [alive (leader player),(vehicle (leader player) == (leader player)),!GV((leader player),xr_pluncon)]];
		_global_pos = [];
		_leader = objNull;
		if (GVAR(with_dynamic_teleport) == 1) then {
			if (GVAR(player_leader_var) != "" && {!isnull (missionNameSpace getVariable GVAR(player_leader_var))}) then {
				_leader = (missionNameSpace getVariable GVAR(player_leader_var));
			};
		} else {
			_leader = leader (group player);
		};
		if (!isnull _leader) then {
			if (alive _leader&& (vehicle _leader == _leader) && !(GV(_leader,xr_pluncon))) then {
				_global_pos = position (_leader);
				_global_pos = (position (_leader)) findEmptyPosition [5,20];
				if (count _global_pos > 0) then {
					_global_pos set [2,0];
				} else {
					_global_pos = (position (_leader));
				};
			};
		};
		_typepos = 0;
	};	
	case 4: {
		_global_pos = [];
		_camps = +(__XJIPGetVar(GVAR(currentcamps)));
		if (!(isNil "_camps") && {count _camps > 0}) then {
			for "_i" from 0 to (count _camps - 1) do {
				if (((_camps select _i) getVariable [QGVAR(SIDE),GVAR(enemy_side)]) != GVAR(own_side_trigger_alt)) then {
					_camps set [_i,objNull];
				};
			};
			_camps = _camps - [objNull];
		};
		diag_log format ["T%1,DT%2,F%3,FPS%4,%5,%6,'%7'", time, diag_tickTime, diag_frameno, diag_fps, THIS_FILE_, __LINE__, [_camps]];
		if (count _camps > 0) then {
			while {count _global_pos <= 0} do {
				_global_pos = (position (_camps call FUNC(randomArrayVal))) findEmptyPosition [5,20];
			};
			_global_pos set [2,0];
		};
		_typepos = 0;
	};
};
GVAR(beam_target) = -1;

// TYRGHEN
if (count _global_pos <= 0) exitwith {};

if (_typepos == 1) then {
	player setDir _global_dir;
	player setPosATL [_global_pos select 0, _global_pos select 1, 0];
} else {
	player setDir _global_dir;
	player setPos _global_pos;
};
0 spawn {
	private "_nobs";
	sleep 2;
	if (GVAR(teleport_dialog_open)) then {closeDialog 0};

	titletext ["", "BLACK IN"];

	_nobs = nearestObjects [player, [[GVAR(the_box)],GVAR(mhq_vehicle_types)] call FUNC(arraypushstack2), 30];
	{player reveal _x} forEach _nobs;
	
	if (GVAR(with_ai) && {alive player} && {!__pGetVar(xr_pluncon)}) then {[] spawn FUNC(moveai)};
};